//
//  MarkingViewController.m
//  DIYCoupon
//
//  Created by 张睿 on 17/1/4.
//  Copyright © 2017年 张睿. All rights reserved.
//

#import "MarkingViewController.h"

@interface MarkingViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>

@property (nonatomic, strong) UISearchBar * customSearchBar;
@property (nonatomic, weak)UITableView * labelTableView;
@property (nonatomic, weak)UILabel * sendLabel;

@end

@implementation MarkingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupUI];
    [self setupBarButton];
}

- (void)viewWillDisappear:(BOOL)animated{
    
    self.customSearchBar.hidden = YES;
    
    [self.customSearchBar resignFirstResponder];
}

#pragma mark - 创建barButton -
- (void)setupBarButton{
    UIBarButtonItem * backItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(popToHomeView)];
    
    self.navigationItem.rightBarButtonItem = backItem;
    
    UIBarButtonItem * previewItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(previewEditImage)];
    
    self.navigationItem.leftBarButtonItem = previewItem;
    
    CGRect mainViewBounds = self.navigationController.view.bounds;
    self.customSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(CGRectGetWidth(mainViewBounds)/2-((CGRectGetWidth(mainViewBounds)-120)/2), CGRectGetMinY(mainViewBounds)+22, CGRectGetWidth(mainViewBounds)-120, 40)];
//    customSearchBar.delegate = self;
    self.customSearchBar.showsCancelButton = NO;
    self.customSearchBar.searchBarStyle = UISearchBarStyleMinimal;
    self.customSearchBar.backgroundColor = [UIColor whiteColor];
    self.customSearchBar.tintColor = [UIColor whiteColor];
    self.customSearchBar.layer.masksToBounds = YES;
    self.customSearchBar.returnKeyType = UIReturnKeyDone;
    [self.navigationController.view addSubview: self.customSearchBar];
    self.customSearchBar.delegate = self;
}


#pragma mark - 取消选择,返回首页 -
- (void)popToHomeView{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)previewEditImage{
    
}

#pragma mark - 页面搭建 -
- (void)setupUI{
    UITableView * labelTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    
    labelTableView.delegate = self;
    labelTableView.dataSource = self;
    
    self.labelTableView = labelTableView;
    
    [self.view addSubview:self.labelTableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        return 5;
    }else{
        return 25;
    }
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    if (indexPath.section == 0) {
        cell.textLabel.text = [NSString stringWithFormat:@"锅德火锅%ld",(long)indexPath.row];//@"锅德火锅";
    }else{
        cell.textLabel.text = [NSString stringWithFormat:@"创橙%ld",(long)indexPath.row];
    }
    
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    
    NSString * sendTest = cell.textLabel.text;
    
    if (self.returnTextBlock != nil) {
        self.returnTextBlock(sendTest);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    
}



- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    if (section == 0) {
        return @"History label";
    }else{
        return @"Hot Label";
    }

}

//设置组头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 30;
    }else{
        return 15;
    }
    
}

#pragma mark - UISearchBar代理方法，监听输入 -

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [self.customSearchBar resignFirstResponder];
    
    if (self.returnTextBlock != nil) {
        self.returnTextBlock(searchBar.text);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - block实现方法 -
- (void)returnText:(ReturnTextBlock)block {
    
    self.returnTextBlock = block;
    
}

@end
